Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

175
Vistas
Trying to save object in useState but getting undefined or Array []

I have been trying to save the result of an API call with a usestate. but I am trying to avoid an interface since the response will be very large and may vary on a case by case basis.

how would I go about saving this response?

const DetailPage = ({navigation, route}) => {
    const [recipeData2, setRecipeData2] = useState();
    const [loading, setLoading] = useState(true);
    const [key, setKey] = useState('APIKEY IS HERE');

    const {id} = route.params;

    console.log(id)

    const getRecipeData2 = async() => {
        setLoading(true);


        const url = 'https://api.spoonacular.com/recipes/' + id + '/information?apiKey=' + key + '&includeNutrition=false';
        axios.get(url).then(function (response) {
            setRecipeData2(response.data);
            console.log(response.data);
            console.log(recipeData2);
        }).catch(function (error) {
            console.log(error);

            if(error) {
                console.log('error')
            }
        });

        setLoading(false);
    }

    useEffect(() => {
        getRecipeData2();
    }, [])

    return (
        <View>
          <Text></Text>  
        </View>
    )
}

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

What it seems is you need to provide initial value to your useState and also you are not setting your loader until your promise is completed for that you can use .finally() block of promise. So doing these things can fix the issue for you. Here's what needs to be done -

const DetailPage = ({ navigation, route }) => {
    const [recipeData2, setRecipeData2] = useState([]);
    const [loading, setLoading] = useState(true);
    const [key, setKey] = useState('APIKEY IS HERE');

    const { id } = route.params;

    const getRecipeData2 = async () => {
        setLoading(true);
        const url = 'https://api.spoonacular.com/recipes/' + id + '/information?apiKey=' + key + '&includeNutrition=false';
        axios.get(url).then(function (response) {
            setRecipeData2(response.data);
        }).catch(function (error) {
            if (error) console.log('error')
        }).finally(function () {
            setLoading(false);
        })
    }

    useEffect(() => {
        getRecipeData2();
    }, [])

    return (
        <View>
            {
                recipeData2.map((data, index) => {
                    return <Text key={ index.toString() }> { data.title } </Text>
                })
            }
        </View>
    )
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda